//
//  NYHomeViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/12/26.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "NYHomeViewController.h"
#import "NYAirlineViewController.h"
#import "NYCourseViewController.h"

#import "NYRefreshFooter.h"

//request
#import "NYHomeEntryRequest.h"
#import "NYHomeNewRequest.h"

//view
#import "NYHomeEntryCell.h"
#import "NYHomeTableSectionView.h"

//model

@interface NYHomeViewController () <QMUITableViewDelegate, NYHomeTableSectionDelegate>


@property (nonatomic, strong) UIImageView *classImageview;
@property (nonatomic, strong) QMUIButton *msgButton;
@property (nonatomic, strong) QMUIButton *gradeButton;
@property (nonatomic, strong) QMUIPopupMenuView *popupView;

@property (nonatomic, strong) NSMutableArray *entryMutaArray;
@property (nonatomic, strong) NSMutableArray *theNewMutaArray;

//是否为精品课程
@property (nonatomic, assign) BOOL isEntry;
//记录刷新是否到底
@property (nonatomic, assign) BOOL entryEnd;
@property (nonatomic, assign) BOOL theNewEnd;

@property (nonatomic, strong) NYHomeEntryRequest *entryRequest;
@property (nonatomic, strong) NYHomeNewRequest *theNewRequest;

@end

@implementation NYHomeViewController

#pragma mark - Life Cycle Methods

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isEntry = true;
    _theNewEnd = false;
    _entryEnd = false;
    [self loadData];
}
- (void)didInitializedWithStyle:(UITableViewStyle)style
{
    [super didInitializedWithStyle:style];
}
#pragma mark - Intial Methods
- (void)initTableView {
    [super initTableView];
    [self.tableView registerClass:[NYHomeTableSectionView class] forHeaderFooterViewReuseIdentifier:[NYHomeTableSectionView className]];

}
- (void)configNimbus {
    [super configNimbus];
    
    self.tableViewModel.delegate = self;
    
    @weakify(self);
    [self.tableViewActions attachToClass:[NYHomeEntryModel class] tapBlock:^BOOL(NYHomeEntryModel *object, id target, NSIndexPath *indexPath) {
        
        @strongify(self);
        
        [NYUserManager pushOrPresentLoginIfNeed:YES inViewController:nil completion:nil dismiss:^{

            if (NYUserManager.isLogin) {
                [self.navigationController pushViewController:[[NYCourseViewController alloc] initWithDetailContentStatus:NYExamineRequestStatusChoose withId:[NSString stringWithFormat:@"%ld",object.id]] animated:true];
            }
        }];
        return true;
    }];
}
#pragma mark - Network Methods
- (void)loadData {
    
    [self configNimbus];
    
    [self setLoading:YES animation:YES];

    [self.tableViewModel removeAll];
    [self.entryMutaArray removeAllObjects];
    [self.theNewMutaArray removeAllObjects];

    [self.entryRequest clearCache];
    [self.theNewRequest clearCache];

    self.theNewEnd = false;
    self.entryEnd = false;
    [self loadTheEntryCourseMethod];
    [self loadTheNewCourseMethod];
}
- (void)loadTheEntryCourseMethod {
    
    @weakify(self);
    [self.entryRequest startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYHomeEntryModel * _Nonnull data) {
        
        @strongify(self);
        NSArray *arrayM = data.list;
        [self.entryMutaArray addObjectsFromArray:arrayM];
        if (self.isEntry) {
            [self.tableViewModel addObjectsFromArray:arrayM];
            [self.tableView reloadData];
            [self setSuccess];
        }
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        [self setError:error];
    }];

}
- (void)loadTheNewCourseMethod {
    
    @weakify(self);
    [self.theNewRequest startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYHomeEntryModel * _Nonnull data) {
        
        @strongify(self);
        NSArray *arrayM = data.list;
        [self.theNewMutaArray addObjectsFromArray:arrayM];
        
        if (!self.isEntry) {
            [self.tableViewModel addObjectsFromArray:arrayM];
            [self.tableView reloadData];
            [self setSuccess];
        }
    } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
        [self setError:error];
    }];
}
- (void)setSuccess {
    [super setSuccess];
    [self configRefreshFooter];
}

- (void)configRefreshFooter {
    
    @weakify(self);
    self.tableView.mj_footer = [NYRefreshFooter footerWithRefreshingBlock:^{

        @strongify(self);
        if (self.isEntry) {
            [[self.entryRequest pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYHomeEntryModel * _Nonnull data) {
                
                @strongify(self);
                NSArray *arrayM = data.list;
                [self.entryMutaArray addObjectsFromArray:arrayM];
                if (self.isEntry) {
                    [self.tableViewModel addObjectsFromArray:arrayM];
                    [self.tableView reloadData];
                    [self setSuccess];
                }

                [arrayM count] == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                self.entryEnd = ([arrayM count] == 0)? true : false;
            } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                
                @strongify(self);
                self.entryEnd = true;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }];
        } else {
            [[self.theNewRequest pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYHomeEntryModel * _Nonnull data) {
                
                @strongify(self);
                NSArray *arrayM = data.list;
                [self.theNewMutaArray addObjectsFromArray:arrayM];
                [self.tableViewModel addObjectsFromArray:arrayM];
                [self.tableView reloadData];
                [arrayM count] == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                self.theNewEnd = ([arrayM count] == 0)? true : false;
            } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                
                @strongify(self);
                self.theNewEnd = true;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }];
        }
    }];
}
#pragma mark - Target Methods
- (void)selectTheGradeEvent:(QMUIButton *)sender {
    //选择年级
    [self.popupView.items enumerateObjectsUsingBlock:^(QMUIPopupMenuItem * _Nonnull item, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([NYUserManager.channelModel.channel_name isEqualToString:item.title]) {
            [item.button setTitleColor:UIColorHex(0x5396FF) forState:(UIControlStateNormal)];
        } else {
            [item.button setTitleColor:UIColorHex(0x070808) forState:(UIControlStateNormal)];
        }
    }];

    [self.popupView layoutWithTargetView:sender];
    [self.popupView showWithAnimated:true];

}
#pragma mark - Public Methods

#pragma mark - Private Method
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
    
    [super configNavigationBar:navigationBar];

    self.navigationItem.titleView = [self.controllerView configNavgationSearchView];
    
    //left
    self.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:[UIView new]],[[UIBarButtonItem alloc] initWithCustomView:self.classImageview]];
    
}
- (BOOL)showChoseChannelIDRightView
{
    return YES;
}

#pragma mark - External Delegate

//MARK: NYHomeTableSectionDelegate
- (void)selectTheNYHomeEntryStatus:(NYHomeEntryStatus)entryType {
    
    [self.tableViewModel removeAll];
    if (entryType == NYHomeEntryExcellent) { //精品课程
        self.isEntry = true;
        [self.tableViewModel addObjectsFromArray:self.entryMutaArray];
        if (self.entryEnd) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
    } else {
        self.isEntry = false;
        [self.tableViewModel addObjectsFromArray:self.theNewMutaArray];
        if (self.theNewEnd) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
    }
    [self.tableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView scrollToRow:0 inSection:0 atScrollPosition:UITableViewScrollPositionTop animated:true];
}
#pragma mark - Nimbus

#pragma mark - QMUITableViewDelegate,QMUITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 46.f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NYHomeEntryStatus entryStatus = _isEntry ? NYHomeEntryExcellent : NYHomeEntryNew;
    NYHomeTableSectionView *view = [[NYHomeTableSectionView alloc] initWithReuseIdentifier:[NYHomeTableSectionView className] delegate:self withEntryStatus:entryStatus];
    return view;
}

#pragma mark - Setter Getter Methods
- (NSMutableArray *)theNewMutaArray {
    if (!_theNewMutaArray) {
        _theNewMutaArray = [NSMutableArray array];
    }
    return _theNewMutaArray;
}
- (NSMutableArray *)entryMutaArray {
    if (!_entryMutaArray) {
        _entryMutaArray = [NSMutableArray array];
    }
    return _entryMutaArray;
}
- (UIImageView *)classImageview {
    if (!_classImageview) {
        _classImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 33)];
        [_classImageview setImage:[UIImage zu_2]];
    }
    return _classImageview;
}
- (NYHomeNewRequest *)theNewRequest {
    if (!_theNewRequest) {
        _theNewRequest = [NYHomeNewRequest requestWithParams:@{kRequestKeyChannel:@(NYUserManager.channelID)}];
    }
    return _theNewRequest;
}
- (NYHomeEntryRequest *)entryRequest {
    if (!_entryRequest) {
        _entryRequest = [NYHomeEntryRequest requestWithParams:@{kRequestKeyChannel:@(NYUserManager.channelID)}];
    }
    return _entryRequest;
}



//MARK: NavigationBar
+ (NYViewControllerNavigationBarType)navigationBarType
{
    return NYViewControllerNavigationBarTypeBlue;
}
@end
